// pages/login/index.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    phone: '',
    verify_code:"",
    car_number:"",
    password:"",
    password_confirmation:"",
		initCountDown: app.globalData.initCountDown, //原始倒计时
		countDown: app.globalData.initCountDown, //倒计时
		checkbox: 'no'
	},
	//绑定手机号动态
	getPhone(e) {
		this.setData({
			phone: e.detail.value,

		})
  },
  getVerifyCode(e){
    this.setData({
			verify_code: e.detail.value,

		})
  },
  getCarNumber(e){
    this.setData({
			car_number: e.detail.value,
		})
  },
  getPassword(e){
    this.setData({
			password: e.detail.value,
		})
  },
  getPasswordConfirm(e){
    this.setData({
			password_confirmation: e.detail.value,
		})
  },

	//验证阿妈发送中
	onCodeDown() {
		let that = this;
		let time = that.data.countDown;
		let setTime;
		clearInterval(setTime)
		time--;
		that.setData({
			countDown: time
		})
		setTime = setInterval(() => {
			time--;

			if (time <= 0) {
				that.setData({
					countDown: that.data.initCountDown
				})
				clearInterval(setTime);
			} else {
				that.setData({
					countDown: time
				})
			}
		}, 1000)
	},
	//获取验证码
	onGetCode() {
		let phone = this.data.phone;
		let that = this;
		if (!phone || phone.length <= 0) {
			wx.showToast({
				title: "请输入手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}

		if (!(/^1[345789]\d{9}$/.test(phone))) {
			wx.showToast({
				title: "请输入正确的手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		this.onCodeDown()
		wx.request({
			url: app.globalData.baseUrl + '/api/getVerifyCode',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token
			},
			data: {
				"phone": phone,
			},
			success: function(res) {
				if (res.data.status) {

				}
			},
			fail: function() {
				console.log("请求失败")
			}
		})
	},
	checkboxChange(e) {
		this.setData({
			checkbox: e.detail.value[0]
		})
	},
	onSubmit(e) {
		let phone = this.data.phone;
		let verify_code = this.data.verify_code;
		let password = this.data.password;
		// let id_card = e.detail.value.id_card;
		let password_confirmation = this.data.password_confirmation;
		let car_number = this.data.car_number;
		// let baodan_order_number = e.detail.value.baodan_order_number;
		let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
		let checkbox = this.data.checkbox;


		if (!phone || phone.length <= 0) {
			wx.showToast({
				title: "请输入手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!verify_code || verify_code.length <= 0) {
			wx.showToast({
				title: "请输入验证码",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		// if (!id_card || id_card.length <= 0) {
		// 	wx.showToast({
		// 		title: "请输入被保险人身份证号",
		// 		icon: 'none',
		// 		duration: 2000
		// 	})
		// 	return false
		// }
		if (!car_number || car_number.length <= 0) {
			wx.showToast({
				title: "请输入车牌号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		// if (!baodan_order_number || baodan_order_number.length <= 0) {
		// 	wx.showToast({
		// 		title: "请输入保单号",
		// 		icon: 'none',
		// 		duration: 2000
		// 	})
		// 	return false
		// }

		if (!password || password.length <= 0) {
			wx.showToast({
				title: "请输入密码",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!password_confirmation || password_confirmation.length <= 0) {
			wx.showToast({
				title: "请输入确认密码",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if(password_confirmation!=password){
			wx.showToast({
				title: "密码不一致",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!(/^1[345789]\d{9}$/.test(phone))) {
			wx.showToast({
				title: "请输入正确的手机号",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!(reg.test(password))) {
			wx.showToast({
				title: "密码必须包含数字和字符且8-16位",
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (checkbox != "yes") {
			wx.showToast({
				title: "请先阅读《用户须知》",
				icon: 'none',
				duration: 2000
			})
			return false
		}

		wx.showLoading({
			title: '提交中',
			mask: true
		})

		wx.request({
			url: app.globalData.baseUrl + '/api/register',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
			data: {
				"phone": phone,
				"verify_code": verify_code,
				"password": password,
				// "id_card": id_card,
				"password_confirmation": password_confirmation,
				"car_number": car_number,
				// "baodan_order_number": baodan_order_number
			},
			success: function(res) {
				wx.hideLoading()


				if (res.data.status) {
					app.globalData.token = res.data.data.records.token;
					// app.globalData.is_finish_wx = res.data.data.records.is_finish_wx;
					// app.globalData.phone = res.data.data.records.phone;
					wx.setStorage({
						key: "token",
						data: res.data.data.records.token,
						success() {
              wx.redirectTo({
                url: '/pages/login/index',
              })
							// wx.switchTab({
							// 	url: '/pages/index/index',
							// })
						}
					})
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

		if (wx.canIUse('hideHomeButton')) {

			wx.hideHomeButton()

		}

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
